<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0, 1)">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPermission(0, 2)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 引用弹窗组件 -->
      <PermissionDialog :formData="formData" :rules="rules" :showDialog="showDialog" :showText="showText"
        @submit="handleSubmit" @cancel="btnCancel" />
    </div>
  </div>
</template>

<script>
import { getPermissionList, updatePermission, addPermission, getPermissionDetail, delPermission } from '@/api/permission';
import { transListToTreeData } from '@/utils';
import PermissionDialog from '@/views/permission/component/PermissionDialog.vue';

export default {
  components: {
    PermissionDialog,
  },
  data() {
    return {
      list: [],
      formData: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0',
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
      },
      showDialog: false,
    };
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增';
    },
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0);
    },
    addPermission(pid, type) {
      this.formData.pid = pid;
      this.formData.type = type;
      this.showDialog = true;
    },
    async editPermission(id) {
      this.formData = await getPermissionDetail(id);
      this.showDialog = true;
    },
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗');
        await delPermission(id);
        this.getPermissionList();
        this.$message.success('删除成功');
      } catch (error) {
        console.log(error);
      }
    },
    handleSubmit(formData) {
      // 这里直接处理提交
      if (formData.id) {
        updatePermission(formData).then(() => {
          this.$message.success('修改成功');
          this.showDialog = false;
          this.getPermissionList();
        });
      } else {
        addPermission(formData).then(() => {
          this.$message.success('新增成功');
          this.showDialog = false;
          this.getPermissionList();
        });
      }
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0',
      };
      this.showDialog = false;
    },
  },
};
</script>

<style scoped>
/* 自定义样式 */
</style>
